package com.wzt.lianfirecontrol.service;

import android.app.Service;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;

import java.io.IOException;

public class PlayMp3Service extends Service {

    private MediaPlayer mediaPlayer;
    private int playTime = 0;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String actionValue = intent.getStringExtra("actionValue");
        openAssetMusics(actionValue);
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 打开assets下的音乐mp3文件
     */
    private void openAssetMusics(String actionValue) {
        switch (actionValue){
            case "0":// 报警
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_BAOJING_STATUS, "1"))){
                    return;
                }
                break;
            case "1":// 故障
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_GUZHANG_STATUS, "1"))){
                    return;
                }
                break;
            case "2":// 监管
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_JIANGUAN_STATUS, "1"))){
                    return;
                }
                break;
            case "3":// 预警
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_YUJING_STATUS, "1"))){
                    return;
                }
                break;
        }
        try {
            String fileName;
            switch (actionValue){
                case "0":
                    fileName = "huojing.mp3";
                    break;
                case "1":
                    fileName = "guzhang.mp3";
                    break;
                case "2":
                    fileName = "jianguan.mp3";
                    break;
                default:
                    fileName = "yujing.mp3";
                    break;
            }
            if (null != mediaPlayer) {
                mediaPlayer.stop();
                mediaPlayer = null;
            }
            //播放 assets/a2.mp3 音乐文件
            AssetFileDescriptor fd = getAssets().openFd(fileName);
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if ("0".equals(actionValue)) {
                        // 火警语音重复播放3次
                        if (playTime == 2) {
                            PlayMp3Service.this.stopSelf();
                        }
                        playTime++;
                        if (null != mediaPlayer) {
                            mediaPlayer.start();
                        }
                    }else {
                        PlayMp3Service.this.stopSelf();
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mediaPlayer) {
            mediaPlayer.stop();
            mediaPlayer = null;
        }
    }
}
